package com.zr.plant.controller;

import com.zr.plant.pojo.PlantAddVo;
import com.zr.plant.pojo.PlantSelectVo;
import com.zr.plant.pojo.PlantUpdateStatusVo;
import com.zr.plant.pojo.PlantUpdateVo;
import com.zr.plant.service.PlantService;
import com.zr.utils.ResultBuilderVo;
import com.zr.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * Created by Lenovo on 2019/12/25.
 */
@RestController
@CrossOrigin
public class PlantController {
    @Autowired
    private PlantService plantService;
    /**
     *  法人下拉框接口
     */
    @GetMapping("legalPerson/ofCurrentUser")
    public ResultVo queryLegalPerson(){
        Integer userId = 1;
        //根据id获取法人信息
        return plantService.queryLegalPersonById(userId);
    }

    /**
     *  分页查询接口
     */
    @PostMapping("legalPlant/query")
    public ResultVo queryPage(@RequestBody PlantSelectVo plantSelectVo){

        return plantService.queryPage(plantSelectVo);
    }
    /**
     *  新增接口
     */
    @PostMapping("legalPlant/add")
    public ResultVo add(@RequestBody @Valid PlantAddVo plantAddVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return plantService.add(plantAddVo);
    }

    /**
     *  修改接口
     */
    @PostMapping("legalPlant/update")
    public ResultVo update(@RequestBody @Valid PlantUpdateVo plantUpdateVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return plantService.updte(plantUpdateVo);
    }

    /**
     *  修改状态接口
     */
    @PostMapping("legalPlant/updatestatus")
    public ResultVo updateStatus(@RequestBody @Valid PlantUpdateStatusVo plantUpdateStatusVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultBuilderVo.error(bindingResult.getFieldError().getDefaultMessage());
        }
        return plantService.updateStatus(plantUpdateStatusVo);
    }
}
